package com.lycx.businessmanager.service.impl;

import java.util.Date;
import java.util.List;

import com.lycx.businessmanager.constant.ConstantsDbOperation;
import com.lycx.businessmanager.constant.ConstantsMessage;
import com.lycx.businessmanager.constant.ConstantsTableName;
import com.lycx.businessmanager.dao.CustomerDao;
import com.lycx.businessmanager.manager.SequenceManager;
import com.lycx.businessmanager.model.Customer;
import com.lycx.businessmanager.service.ICustomerService;

public class CustomerServiceImpl implements ICustomerService {
	
	private CustomerDao customerDao;
	
	public void setCustomerDao(CustomerDao customerDao){
		this.customerDao = customerDao;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> selectForPage(Customer customer,int startNum,int limit) {
		List<Customer> result = null;
		try{
			result = customerDao.selectForPagination(ConstantsDbOperation.CUSTOMER_SELECT, customer,startNum,limit);
		}catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public String add(Customer customer) {
		String result = null;
		try {
			List<Customer> list = selectRepeat(customer);
			if (list != null && list.size() > 0) {
				result = ConstantsMessage.CUSTOMER_REPEAT_FAIL;
			} else {
				customer.setId(SequenceManager.createId(ConstantsTableName.CUSTOMER));
				customer.setCreateTime(new Date());
				customer.setModifyTime(new Date());
				customerDao.insert(ConstantsDbOperation.CUSTOMER_INSERT, customer);
				result = ConstantsMessage.CUSTOMER_ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.CUSTOMER_ADD_FAIL;
		}
		return result;
	}

	@Override
	public String del(int[] ids) {
		String result = null;
		try {
			if (ids != null && ids.length > 0) {
				for (int id : ids) {
					Customer customer = new Customer();
					customer.setId(id);
					customerDao.delete(ConstantsDbOperation.CUSTOMER_DELETE, customer);
				}
				result = ConstantsMessage.CUSTOMER_DEL_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = ConstantsMessage.CUSTOMER_DEL_FAIL;
		}
		return result;
	}

	@Override
	public String mod(Customer customer) {
		String result = null;
		try{
			List<Customer> list = selectRepeat(customer);
			if (list != null && list.size() > 0) {
				result = ConstantsMessage.CUSTOMER_REPEAT_FAIL;
			} else {
				customer.setModifyTime(new Date());
				customerDao.update(ConstantsDbOperation.CUSTOMER_UPDATE, customer);
		        result = ConstantsMessage.CUSTOMER_MOD_SUCCESS;
			}
		    
		}catch(Exception e){
			e.printStackTrace();
			result = ConstantsMessage.CUSTOMER_MOD_FAIL;
		}
        return result;
	}

	@Override
	public int selectTotalCount(Customer customer) {
		return (Integer)customerDao.selectOne(ConstantsDbOperation.CUSTOMER_SELECT_TOTAL_COUNT, customer);
	}
	
	@SuppressWarnings("unchecked")
	private List<Customer> selectRepeat(Customer customer) {
		return (List<Customer>) customerDao.select(ConstantsDbOperation.CUSTOMER_SELECT_REPEAT, customer);
	}

}
